/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ut_frstat_server_count.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <unistd.h>
#include <sys/time.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frame_params.h"
#include "frstat_server.h"
#include "apmtest_cunit.h"

extern int g_i_flag_run;
extern unsigned long g_ul_pcap_queue_used;
extern Frapm_params st_apm_params;

int __frhash_make_key(u_int ui_ip, u_short us_port, int i_l7_proto
        , char *p_key, unsigned int ui_key_size);

typedef struct __conn_info {
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
} conn_info;

typedef struct __conn_avg {
    unsigned long ul_connections[4];
    unsigned long ul_requests[4];
    unsigned long ul_responses[4];
} conn_avg;

typedef struct __server_info {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    struct timeval start_time;
    conn_info st_conns_total;
    conn_avg  st_conns_avg;
} Server_info;

//端口关联表
typedef struct __port_related {
    u_int   ui_ip;
    u_short us_port;
    u_int   ui_ip_related;
    u_short us_port_related;
    
    struct timeval start_time;
} Port_related;

//做完关联后的hash节点
typedef struct __server_info_avg {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
    
    unsigned long ul_connections_avg;
    unsigned long ul_requests_avg;
    unsigned long ul_responses_avg;
} Server_info_avg; 

extern Frhash_header g_st_hash_server;

static int __suite_init(void)
{
    g_i_flag_run = 1;
    g_ul_pcap_queue_used = 0;
    st_apm_params.ui_stat_server_hash_size = 10;
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

static void case1(void)
{
    Frlist_node *p_st_node = NULL;
    Server_info *p_server_info = NULL;
    char arr_hash_key[25] = {'\0'};
    int i_hash_key_len = 0;
    
    frstat_server_init(1);
    
    i_hash_key_len = __frhash_make_key(16781484, 80, 1433, arr_hash_key, sizeof(arr_hash_key));
    
    p_server_info = (Server_info *)frmalloc(sizeof(Server_info));
    
    memset(p_server_info, 0x00, sizeof(Server_info));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    
    p_server_info->st_conns_total.ul_connections = 300;
    p_server_info->st_conns_total.ul_requests = 200;
    p_server_info->st_conns_total.ul_responses = 100;
    
    p_server_info->st_conns_avg.ul_connections[0] = 280;
    p_server_info->st_conns_avg.ul_requests[0] = 180;
    p_server_info->st_conns_avg.ul_responses[0] = 80;
    p_server_info->st_conns_avg.ul_connections[1] = 200;
    p_server_info->st_conns_avg.ul_requests[1] = 150;
    p_server_info->st_conns_avg.ul_responses[1] = 70;
    p_server_info->st_conns_avg.ul_connections[2] = 150;
    p_server_info->st_conns_avg.ul_requests[2] = 120;
    p_server_info->st_conns_avg.ul_responses[2] = 60;
    p_server_info->st_conns_avg.ul_connections[3] = 130;
    p_server_info->st_conns_avg.ul_requests[3] = 100;
    p_server_info->st_conns_avg.ul_responses[3] = 50;
    
    p_st_node = frlist_node_create(p_server_info);
    
    frhash2_addStr(&g_st_hash_server, arr_hash_key, i_hash_key_len, p_st_node);
    
    frstat_server_count();
    
    frstat_server_output(NULL, NULL);
    
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[0],300);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[0],200);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[0],100);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[1],280);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[1],180);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[1],80);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[2],200);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[2],150);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[2],70);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[3],150);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[3],120);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[3],60);
    
    frstat_server_destory();
    
    return;

}

static void case2(void)
{
    Frlist_node *p_st_node = NULL;
    Server_info *p_server_info = NULL;
    Server_info *p_server_info2 = NULL;
    char arr_hash_key[25] = {'\0'};
    int i_hash_key_len = 0;
    
    frstat_server_init(1);
    
    i_hash_key_len = __frhash_make_key(16781484, 80, 1433, arr_hash_key, sizeof(arr_hash_key));
    
    p_server_info = (Server_info *)frmalloc(sizeof(Server_info));
    
    memset(p_server_info, 0x00, sizeof(Server_info));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    
    p_server_info->st_conns_total.ul_connections = 300;
    p_server_info->st_conns_total.ul_requests = 200;
    p_server_info->st_conns_total.ul_responses = 100;
    
    p_server_info->st_conns_avg.ul_connections[0] = 280;
    p_server_info->st_conns_avg.ul_requests[0] = 180;
    p_server_info->st_conns_avg.ul_responses[0] = 80;
    p_server_info->st_conns_avg.ul_connections[1] = 200;
    p_server_info->st_conns_avg.ul_requests[1] = 150;
    p_server_info->st_conns_avg.ul_responses[1] = 70;
    p_server_info->st_conns_avg.ul_connections[2] = 150;
    p_server_info->st_conns_avg.ul_requests[2] = 120;
    p_server_info->st_conns_avg.ul_responses[2] = 60;
    p_server_info->st_conns_avg.ul_connections[3] = 130;
    p_server_info->st_conns_avg.ul_requests[3] = 100;
    p_server_info->st_conns_avg.ul_responses[3] = 50;
    
    p_st_node = frlist_node_create(p_server_info);
    
    frhash2_addStr(&g_st_hash_server, arr_hash_key, i_hash_key_len, p_st_node);
    
    memset(arr_hash_key, 0x00, sizeof(arr_hash_key));
    i_hash_key_len = __frhash_make_key(16781484, 8080, 1455, arr_hash_key, sizeof(arr_hash_key));
    
    p_server_info2 = (Server_info *)frmalloc(sizeof(Server_info));
    
    memset(p_server_info2, 0x00, sizeof(Server_info));
    
    p_server_info2->ui_ip = 16781484;
    p_server_info2->us_port = 8080;
    p_server_info2->i_l7_proto = 1455;
    
    p_server_info2->st_conns_total.ul_connections = 500;
    p_server_info2->st_conns_total.ul_requests = 250;
    p_server_info2->st_conns_total.ul_responses = 250;
    
    p_server_info2->st_conns_avg.ul_connections[0] = 480;
    p_server_info2->st_conns_avg.ul_requests[0] = 220;
    p_server_info2->st_conns_avg.ul_responses[0] = 230;
    p_server_info2->st_conns_avg.ul_connections[1] = 450;
    p_server_info2->st_conns_avg.ul_requests[1] = 190;
    p_server_info2->st_conns_avg.ul_responses[1] = 200;
    p_server_info2->st_conns_avg.ul_connections[2] = 430;
    p_server_info2->st_conns_avg.ul_requests[2] = 170;
    p_server_info2->st_conns_avg.ul_responses[2] = 180;
    p_server_info2->st_conns_avg.ul_connections[3] = 420;
    p_server_info2->st_conns_avg.ul_requests[3] = 150;
    p_server_info2->st_conns_avg.ul_responses[3] = 150;
    
    p_st_node = frlist_node_create(p_server_info2);
    
    frhash2_addStr(&g_st_hash_server, arr_hash_key, i_hash_key_len, p_st_node);
    
    frstat_server_count();
    
    frstat_server_output(NULL, NULL);
    
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[0],300);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[0],200);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[0],100);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[1],280);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[1],180);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[1],80);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[2],200);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[2],150);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[2],70);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_connections[3],150);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_requests[3],120);
    CU_ASSERT_EQUAL(p_server_info->st_conns_avg.ul_responses[3],60);
    
    
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_connections[0],500);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_requests[0],250);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_responses[0],250);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_connections[1],480);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_requests[1],220);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_responses[1],230);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_connections[2],450);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_requests[2],190);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_responses[2],200);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_connections[3],430);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_requests[3],170);
    CU_ASSERT_EQUAL(p_server_info2->st_conns_avg.ul_responses[3],180);
    
    frstat_server_destory();
    
    return;
}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("ut_frstat_server_count 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}